package concurrecy;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ShutdownNowDemo {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newCachedThreadPool();
        for(int i = 0; i < 2; i ++)
            service.execute(() -> {
                int n = 0;
                while(true) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread() + " : " + n ++);
                    } catch (InterruptedException e) {
                        System.out.println("Interrupted");
                        break;
                    }
                }
                System.out.println("Exit from run");
            });
        service.execute(() -> {
            double d = 1;
            while(!Thread.interrupted()) {
                System.out.println("start math calc");
                for (int i = 0; i < 50000000; i++) {
                    d *= Math.E / Math.PI;
                }
                System.out.println("math calc end");
            }
            System.out.println("exit from while");
        });
        TimeUnit.SECONDS.sleep(3);
        List<Runnable> runnables = service.shutdownNow();
        System.out.println(runnables.size());
        for (Runnable runnable : runnables) {
            System.out.println(runnable);
        }
    }
}
